//
//  MyMusicViewController.m
//  CatMusic
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyMusicViewController.h"
#import "MusicPlayViewController.h"
#import "DataBaseHandle.h"
#import "NightMode.h"
#import "PlayerSingleton.h"
#import "MyLoveListViewController.h"
#import "TryListenViewController.h"
#import "TimerViewController.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "AFNetworkReachabilityManager.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface MyMusicViewController ()<UIAlertViewDelegate>
@property (nonatomic , retain)UIView *myView;       // 抽屉上层view
@property (nonatomic , assign)BOOL result;          // 判断是否点击设置按钮
@property (nonatomic , assign)UISwipeGestureRecognizer *swipeRight;     // 右手势
@property (nonatomic , assign)UISwipeGestureRecognizer *swipeLeft;      // 左手势
@property (nonatomic , retain)UIButton *logButton;                      // 登录按钮
@property (nonatomic , retain)UIButton *auditionButton;                 // 试听按钮
@property (nonatomic ,retain)UIButton  *MyLoveButtion;                  // 我喜欢按钮
@property(nonatomic, assign) BOOL nightStatu;                           // 夜间状态
@property(nonatomic, retain)UIButton *nightButton;                      // 夜间模式按钮
@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UIImageView *headImageView;                 // 登录头像
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, assign)BOOL isLogin;                               // 判断是否登录
@property(nonatomic, retain)UIButton *leftButton;
@property(nonatomic, retain)UIImageView *backgroundImag;

@property (nonatomic , retain)UIImageView *backgroundImage;

@end

@implementation MyMusicViewController

- (void)dealloc
{
    [_myView release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.result = YES;
    [self.view setBackgroundColor:[UIColor redColor]];
    self.title = @"我的音乐";

    
    
    
    
    // 监听是否是夜间模式
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDayModel) name:@"LightDayNSNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDayModel) name:@"NightDayNSNotification" object:nil];
    
    if ([NightMode shareNightMode].isNight) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }

    // 抽屉背景图片
    self.backgroundImag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 50)];
    self.backgroundImag.image = [UIImage imageNamed:@"chouTi"];
    [self.view addSubview:self.backgroundImag];


    
    // 定时关机
    UIButton *nstimerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [nstimerButton setFrame:CGRectMake(WIDTH /375 * 100, HEIGHT / 667 * 180, WIDTH /375 * 150, HEIGHT / 667 * 40)];
    [nstimerButton setTitle:@"睡眠定时" forState:UIControlStateNormal];
    [self.view addSubview:nstimerButton];
    nstimerButton.layer.borderWidth = 1;
    nstimerButton.layer.cornerRadius = 5;
    [nstimerButton addTarget:self action:@selector(nstimerButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 夜间模式
    self.nightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.nightButton setFrame:CGRectMake(WIDTH /375 * 100, HEIGHT / 667 * 260, WIDTH /375 * 150, HEIGHT / 667 * 40)];
    [self.nightButton setTitle:@"夜间模式" forState:UIControlStateNormal];
    [self.view addSubview:self.nightButton];
    self.nightButton.layer.borderWidth = 1;
    self.nightButton.layer.cornerRadius = 5;
    [self.nightButton addTarget:self action:@selector(nightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 版权信息
    UIButton *copyrightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [copyrightButton setFrame:CGRectMake(WIDTH /375 * 100, HEIGHT / 667 * 340, WIDTH /375 * 150, HEIGHT / 667 * 40)];
    [copyrightButton setTitle:@"关于我们" forState:UIControlStateNormal];
    [self.view addSubview:copyrightButton];
    copyrightButton.layer.borderWidth = 1;
    copyrightButton.layer.cornerRadius = 5;
    [copyrightButton addTarget:self action:@selector(copyrightButtonAction) forControlEvents:UIControlEventTouchUpInside];

    
    
    [self creatSwipe];      // 添加手势
    
    
    
    // 设置左边按钮 
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(leftButtonAction:)];
    [self setBackgroundAndButton];
    
}




#pragma mark  添加手势
- (void)creatSwipe
{
    self.swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRightAction:)];
    self.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:self.swipeRight];
    [self.swipeRight release];
    
    self.swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeftAction:)];
    self.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:self.swipeLeft];
    [self.swipeLeft release];

}


// 定时器
- (void)nstimerButtonAction
{
//     TimerViewController *timer = [[TimerViewController alloc] init];
//    [self.navigationController pushViewController:timer animated:YES];
//    [timer release];
    
         TimerViewController *timer = [[TimerViewController alloc] init];
//    [timer setModalTransitionStyle:UIModalTransitionStylePartialCurl];
//        [self.navigationController presentViewController:timer animated:YES completion:nil];
    [self.navigationController pushViewController:timer animated:YES ];
        [timer release];
    
}

// 夜间模式
- (void)nightButtonAction
{
    self.nightStatu = !self.nightStatu;
    if (self.nightStatu) {
        [self.nightButton setTitle:@"夜间模式" forState:UIControlStateNormal];
//        self.backgroundImag.image = [UIImage imageNamed];
        
    } else {
        [self.nightButton setTitle:@"日间模式" forState:UIControlStateNormal];
    }
    
    [NightMode shareNightMode].isNight = ![NightMode shareNightMode].isNight;
    [[NightMode shareNightMode] saveNightModeInfoToDisk];
}

// 版权信息
- (void)copyrightButtonAction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版权声明" message:@"本软件刊载的所有内容，包括文字、图片、音频、视频、软件、程序、以及网页版式设计等均在网上搜集。\n访问者可将本网站提供的内容或服务用于个人学习、研究或欣赏，以及其他非商业性或非盈利性用途，但同时应遵守著作权法及其他相关法律的规定，不得侵犯本网站及相关权利人的合法权利。除此以外，将本网站任何内容或服务用于其他用途时，须征得本网站及相关权利人的书面许可，并支付报酬\n本网站内容原作者如不愿意在本网站刊登内容，请及时通知本站，予以删除。\n电子邮箱：1466525803@qq.com" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    
}



// 背景图片和按钮
- (void)setBackgroundAndButton
{
   
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH * 0, HEIGHT * 0, WIDTH, HEIGHT)];
    self.backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, HEIGHT / 667 * -30, WIDTH, HEIGHT)];
    self.backgroundImage.image = [UIImage imageNamed:@"cat.jpg"];
    [self.myView addSubview:self.backgroundImage];
    
    
    self.auditionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.auditionButton.frame= CGRectMake(WIDTH / 375 * 10, HEIGHT / 667 * 100, WIDTH / 375 *355, HEIGHT / 667 *60);
    [self.auditionButton setTitle:@"试听列表" forState:UIControlStateNormal];
    [self.auditionButton addTarget:self action:@selector(listenAction:) forControlEvents:UIControlEventTouchUpInside];
    self.auditionButton.layer.borderWidth = 1;
    self.auditionButton.layer.cornerRadius = 5;
    [self.myView addSubview:self.auditionButton];
    
    self.MyLoveButtion = [UIButton buttonWithType:UIButtonTypeSystem];
    self.MyLoveButtion.frame= CGRectMake(WIDTH / 375 * 10, HEIGHT / 667 *200, WIDTH / 375 * 355, HEIGHT / 667 * 60);
    [self.MyLoveButtion setTitle:@"我喜欢" forState:UIControlStateNormal];
    [self.MyLoveButtion addTarget:self action:@selector(myLoveAction:) forControlEvents:UIControlEventTouchUpInside];
    self.MyLoveButtion.layer.borderWidth = 1;
    self.MyLoveButtion.layer.cornerRadius = 5;
    [self.myView addSubview:self.MyLoveButtion];
    
    [self.view addSubview:self.myView];
    
}

#pragma mark - 动画滑动效果
- (void)animationDidAction
{
    if (self.result == YES) {
        [self swipeRightAction:self.swipeRight];
    }else if (self.result == NO){
        [self swipeLeftAction:self.swipeLeft];
    }
    self.result = !self.result;
}

- (void)swipeRightAction:(UISwipeGestureRecognizer *)right
{
    [UIView animateWithDuration:0.25 animations:^{
        self.myView.frame = CGRectMake(WIDTH / WIDTH * 300, 0, WIDTH / WIDTH * 75, HEIGHT / HEIGHT * 400);
    }];
    
}

- (void)swipeLeftAction:(UISwipeGestureRecognizer *)left
{
    [UIView animateWithDuration:0.25 animations:^{
        self.myView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    }];
}



- (void)leftButtonAction:(UIButton *)button
{
    [self animationDidAction];
}

- (void)listenAction:(UIButton *)button
{
    TryListenViewController *tryList = [[TryListenViewController alloc] init];
    [self makeTryListenFile];
    [self.navigationController pushViewController:tryList animated:YES];
    [tryList release];
}
- (void)myLoveAction:(UIButton *)button
{
        MyLoveListViewController *myLoveVC = [[MyLoveListViewController alloc] init];
        // 反归档
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
        NSString *sandBoxPath = [sandBox lastObject];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *musicListPath = [sandBoxPath stringByAppendingPathComponent:@"collect"];
        NSArray *arrList = [fileManager contentsOfDirectoryAtPath:musicListPath error:nil];
        NSMutableArray *musicList = [NSMutableArray array];
        for (NSInteger i = 0; i < arrList.count; i++) {
            NSString *musicInfoPath = [musicListPath stringByAppendingPathComponent:arrList[i]];
            BaseModel *music = [NSKeyedUnarchiver unarchiveObjectWithFile:musicInfoPath];
            [musicList addObject:music];
        }
        myLoveVC.arr = musicList;
        [self.navigationController pushViewController:myLoveVC animated:YES];
        [myLoveVC release];
}

- (void)makeTryListenFile
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *tryListenListPath = [sandBoxPath stringByAppendingPathComponent:@"tryListen.plist"];
    NSMutableArray *musicList = [NSMutableArray array];
    musicList = [NSKeyedUnarchiver unarchiveObjectWithFile:tryListenListPath];
    [PlayerSingleton sharePlayerSingleton].tryListenArr = musicList;
}


- (void)setLightDayModel
{
    self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:245 / 255.0 green:241/ 255.0 blue:143 / 255.0 alpha:1];//////
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:245 / 255.0 green:241/ 255.0 blue:143 / 255.0 alpha:1];//////
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIColor * color = [UIColor blackColor];
    //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
     self.backgroundImag.image = [UIImage imageNamed:@"chouTi"];
    self.backgroundImage.image = [UIImage imageNamed:@"cat.jpg"];
    self.auditionButton.frame= CGRectMake(WIDTH / 375 * 10, HEIGHT / 667 * 100, WIDTH / 375 *355, HEIGHT / 667 *60);
    self.MyLoveButtion.frame= CGRectMake(WIDTH / 375 * 10, HEIGHT / 667 *200, WIDTH / 375 * 355, HEIGHT / 667 * 60);
}

- (void)setNightDayModel
{
    self.view.backgroundColor = [UIColor colorWithRed:38 / 255.0 green:38/ 255.0 blue:38 / 255.0 alpha:1];
    
    self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:38 / 255.0 green:38/ 255.0 blue:38 / 255.0 alpha:1];

    
    
    UIColor * color = [UIColor whiteColor];
    //这里我们设置的是颜色，还可以设置shadow等，具体可以参见api
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
  self.backgroundImag.image = [UIImage imageNamed:@"night"];
    self.backgroundImage.image = [UIImage imageNamed:@"backgrondImagezhuye"];
    
    self.auditionButton.frame = CGRectMake(WIDTH / 375 * 30, HEIGHT / 667 * 370, WIDTH / 375 * 100, HEIGHT / 667 * 40);
    self.MyLoveButtion.frame = CGRectMake(WIDTH / 375 * 30, HEIGHT / 667 * 430, WIDTH / 375 * 100, HEIGHT / 667 * 40);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
